<?php
/**
 * Abstract base class for exceptions used to (optionally) wrap another
 * exception. Exception wrapping is useful for providing a meaningful message
 * to the layers above on a otherwise meaningless exception.
 * 
 * @package be.appcraft.util
 * @version $Revision: 1.1 $
 *
 * @author Stijn Van Bael
 * @since 1.0
 */
abstract class Pageley_Util_NestedException extends Exception {
	
	/**
	 * The exception wrapped by this exception.
	 *
	 * @var Exception
	 */
	private $cause;
	
	/**
	 * Creates a new NestedException.
	 *
	 * @param string $message: a message describing the exception or null to 
	 * provide no message
	 * @param int $code: an error code or null to provide no error code
	 * @param Exception $cause: the exception that caused this exception or
	 * null to provide no cause
	 */
	public function __construct($message = null, $code = 0, $cause = null) {
		parent::__construct($message, $code);
		$this->cause = $cause;
	}
	
	/**
	 * Gets the exception wrapped by this exception.
	 *
	 * @return the exception wrapped by this exception
	 */
	public function getCause() {
		return $this->cause;
	}
	
	public function __toString() {
		return parent::__toString() . ($this->cause != null ? 
			"\n\nCaused by...\n" . $this->cause->__toString()
			: "");
	}
}